package jp.heartdokyun.adapter;

import java.util.ArrayList;

import jp.heartdokyun.lazy.ImageLoader;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
	private Context mContext;
	private ArrayList<Integer> mListImageIDs;
	private int mCellWidth;
	public ImageLoader mImageLoader;

	public ImageAdapter(Context c, ArrayList<Integer> listImageIDs) {
		mContext = c;
		mListImageIDs = listImageIDs;
		mImageLoader = new ImageLoader(mContext.getApplicationContext());
		Display display = ((WindowManager) mContext
				.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		// mCellHeight = (int) (display.getHeight() / 3);
		mCellWidth = display.getWidth() / 3;
	}

	public int getCount() {
		return mListImageIDs.size();
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	// create a new ImageView for each item referenced by the Adapter
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageView;
		BitmapFactory.Options bo = new BitmapFactory.Options();
		bo.inJustDecodeBounds = true;
		BitmapFactory.decodeResource(mContext.getResources(),
				mListImageIDs.get(position), bo);
		if (convertView == null) { // if it's not recycled, initialize some
									// attributes
			imageView = new ImageView(mContext);
			imageView.setLayoutParams(new GridView.LayoutParams(mCellWidth, bo.outHeight));
			// imageView
			// .setLayoutParams(new GridView.LayoutParams(mCellWidth, 156));
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			imageView.setPadding(3, 0, 3, 1);
		} else {
			imageView = (ImageView) convertView;
		}
		// BitmapFactory.Options opts = new BitmapFactory.Options();
		// opts.inSampleSize = 4;
		mImageLoader.DisplayImage(mListImageIDs.get(position), imageView);
		// imageView.setImageBitmap(bmp);
		return imageView;
	}
	// references to our images
	// private Integer[] mThumbIds = { R.drawable.sample, R.drawable.sample,
	// R.drawable.sample, R.drawable.sample, R.drawable.sample,
	// R.drawable.sample, R.drawable.sample, R.drawable.sample,
	// R.drawable.sample, R.drawable.sample, R.drawable.sample,
	// R.drawable.sample, R.drawable.sample, R.drawable.sample,
	// R.drawable.sample, R.drawable.sample, R.drawable.sample,
	// R.drawable.sample, R.drawable.sample, R.drawable.sample,
	// R.drawable.sample, R.drawable.sample };
}
